package com.example.peibei.ui.activity;

import android.Manifest;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingtao.common.bean.User;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.dingtao.common.util.UIUtils;
import com.dingtao.common.util.view.OnOffView;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.bean.SpUserInfoCall;
import com.example.peibei.service.presenter.UserInfoPresenter;
import com.example.peibei.service.presenter.YinsiCheckPresenter;
import com.linxiao.framework.dialog.BottomDialog;
import com.tencent.mmkv.MMKV;
import com.yxp.permission.util.lib.PermissionUtil;
import com.yxp.permission.util.lib.callback.PermissionResultCallBack;

import butterknife.BindView;
import butterknife.OnClick;

public class PrivacySettingActivity extends WDActivity {
    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.switch_dyn)
    OnOffView switch_dyn;
    @BindView(R.id.switch_fans)
    OnOffView switch_fans;
    @BindView(R.id.switch_ord)
    OnOffView switch_ord;
    @BindView(R.id.switch_camera)
    OnOffView switch_camera;
    @BindView(R.id.switch_push)
    OnOffView switch_push;
    @BindView(R.id.switch_voice)
    OnOffView switch_voice;
    @BindView(R.id.switch_adress)
    OnOffView switch_adress;
    @BindView(R.id.tv_cache)
    TextView tv_cache;
    private UserInfoPresenter userInfoPresenter;
    private YinsiCheckPresenter yinsiCheckPresenter;
    private SPUtils spUtils;

    String token;
    String status = "0";
    private static String[] PERMISSIONSLOCAL = {
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
            Manifest.permission.ACCESS_FINE_LOCATION
    };
    private static String[] PERMISSIONSCAREAM = {
            Manifest.permission.CAMERA,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
    };
    private static String[] PERMISSIONSVOICE = {
            Manifest.permission.RECORD_AUDIO
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_privacy_setting;
    }

    @Override
    protected void initView() {
        spUtils = new SPUtils(this, SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        switch_dyn.setCheckBoxCall(new OnOffView.CheckBoxCall() {
            @Override
            public void check(boolean isOff) {
                if (isOff) {
                    yinsiCheckPresenter.reqeust("Bearer " + token, "0", "2");
                } else {
                    yinsiCheckPresenter.reqeust("Bearer " + token, "1", "2");
                }
            }
        });
        switch_ord.setCheckBoxCall(new OnOffView.CheckBoxCall() {
            @Override
            public void check(boolean isOff) {//	1查看我发布的订单 2查看我的动态 3 查看我的粉丝
                if (isOff) {
                    yinsiCheckPresenter.reqeust("Bearer " + token, "0", "1");
                } else {
                    yinsiCheckPresenter.reqeust("Bearer " + token, "1", "1");
                }
            }
        });
        switch_fans.setCheckBoxCall(new OnOffView.CheckBoxCall() {
            @Override
            public void check(boolean isOff) {//	1查看我发布的订单 2查看我的动态 3 查看我的粉丝
                if (isOff) {
                    yinsiCheckPresenter.reqeust("Bearer " + token, "0", "3");
                } else {
                    yinsiCheckPresenter.reqeust("Bearer " + token, "1", "3");
                }
            }
        });
        checkLocationPermission();
        switch_camera.setCheckBoxCall(new OnOffView.CheckBoxCall() {
            @Override
            public void check(boolean isOff) {
                if (isOff) {

                } else {
                    getPermission(0, PERMISSIONSCAREAM);
                }
            }
        });
        switch_voice.setCheckBoxCall(new OnOffView.CheckBoxCall() {
            @Override
            public void check(boolean isOff) {
                if (isOff) {

                } else {
                    getPermission(1, PERMISSIONSCAREAM);
                }
            }
        });
        switch_adress.setCheckBoxCall(new OnOffView.CheckBoxCall() {
            @Override
            public void check(boolean isOff) {
                if (isOff) {

                } else {
                    getPermission(2, PERMISSIONSVOICE);
                }
            }
        });
        switch_push.setCheckBoxCall(new OnOffView.CheckBoxCall() {
            @Override
            public void check(boolean isOff) {
                MMKV.defaultMMKV().encode("orientation_push_new",!isOff);
            }
        });
        userInfoPresenter = new UserInfoPresenter(new UserInfoCall());
        yinsiCheckPresenter = new YinsiCheckPresenter(new Yinsicheck());
        userInfoPresenter.reqeust("Bearer " + token);
        boolean push = MMKV.defaultMMKV().decodeBool("orientation_push_new", false);
        switch_push.setDefOff(push);
    }

    private void checkLocationPermission() {
        int i = PermissionUtil.getInstance().checkSinglePermission(Manifest.permission.CAMERA);
        switch_camera.setDefOff(i == 1);
        int j = PermissionUtil.getInstance().checkSinglePermission(Manifest.permission.RECORD_AUDIO);
        switch_voice.setDefOff(j == 1);
        int k = PermissionUtil.getInstance().checkSinglePermission(Manifest.permission.ACCESS_COARSE_LOCATION);
        int l = PermissionUtil.getInstance().checkSinglePermission(Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS);
        int m = PermissionUtil.getInstance().checkSinglePermission(Manifest.permission.ACCESS_FINE_LOCATION);
        switch_adress.setDefOff(k == 1 || l == 1 || m == 1);
    }

    private void getPermission(int type, String[] PERMISSIONS) {
        PermissionUtil.getInstance().request(this, PERMISSIONS,
                new PermissionResultCallBack() {
                    @Override
                    public void onPermissionGranted() {

                    }

                    @Override
                    public void onPermissionGranted(String... strings) {
                        // 当所有权限的申请被用户同意之后,该方法会被调用
                        //get_NovelUrl(id);
                    }

                    @Override
                    public void onPermissionDenied(String... permissions) {

                        UIUtils.showToastSafe("部分权限被禁止需要手动开启");
                        if (type == 0) {
                            switch_camera.setDefOff(false);
                        } else if (type == 1) {
                            switch_voice.setDefOff(false);
                        } else if (type == 2) {
                            switch_adress.setDefOff(false);
                        }
                        // 当权限申请中的某一个或多个权限,被用户曾经否定了,并确认了不再提醒时,也就是权限的申请窗口不能再弹出时,该方法将会被调用
                    }

                    @Override
                    public void onRationalShow(String... permissions) {
                        UIUtils.showToastSafe("部分权限被禁止需要手动开启");
                        // 当权限申请中的某一个或多个权限,被用户否定了,但没有确认不再提醒时,也就是权限窗口申请时,但被否定了之后,该方法将会被调用.
                    }

                });
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    @OnClick(R.id.rl_clean_cache)
    public void seeMe() {
        showChoosePayAccount();
    }

    @OnClick(R.id.rl_heimingdan)
    public void heimingdan() {

    }

    @OnClick(R.id.tv_user_Agree)
    public void userAgree() {
        Bundle bundle = new Bundle();
        bundle.putString("type", "privateAgree");
        intent(UserAgreeActivity.class, bundle);
    }

    private void showChoosePayAccount() {
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_choose_see, null);
        BottomDialog bottomDialog = new BottomDialog(this);
        bottomDialog.setContentView(dialogView);
        bottomDialog.setCanceledOnTouchOutside(true);
        bottomDialog.show();
        Button tv_submit = dialogView.findViewById(R.id.tv_submit);
        RelativeLayout rl_zfb = dialogView.findViewById(R.id.rl_zfb);
        RelativeLayout rl_wx = dialogView.findViewById(R.id.rl_wx);
        ImageView iv_choose_zfb = dialogView.findViewById(R.id.iv_choose_zfb);
        ImageView iv_choose_wx = dialogView.findViewById(R.id.iv_choose_wx);
        if (!TextUtils.isEmpty(status) && status.equals("0")) {
            iv_choose_zfb.setVisibility(View.INVISIBLE);
            iv_choose_wx.setVisibility(View.VISIBLE);
        } else {
            iv_choose_zfb.setVisibility(View.VISIBLE);
            iv_choose_wx.setVisibility(View.INVISIBLE);
        }
        rl_zfb.setOnClickListener(v -> {
            status = "1";
            yinsiCheckPresenter.reqeust("Bearer " + token, "1", "3");
            tv_cache.setText("所有人可见");
            bottomDialog.dismiss();
        });
        rl_wx.setOnClickListener(v -> {
            status = "0";
            yinsiCheckPresenter.reqeust("Bearer " + token, "0", "3");
            tv_cache.setText("仅自己");
            bottomDialog.dismiss();
        });
        tv_submit.setOnClickListener(v -> {
            bottomDialog.dismiss();
        });
    }

    @Override
    protected void destoryData() {

    }

    class UserInfoCall extends SpUserInfoCall {

        @Override
        public void success(User data, Object... args) {
            super.success(data,args);
            switch_dyn.setDefOff(data.getEnableDynamic() == 1);
            switch_fans.setDefOff(data.getEnableFollower() == 1);
            tv_cache.setText(data.getEnableFollower() == 1 ? "所有人可见" : "仅自己");
            status = String.valueOf(data.getEnableFollower());
            switch_ord.setDefOff(data.getEnableOrder() == 1);
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }

    class Yinsicheck implements DataCall<User> {

        @Override
        public void success(User data, Object... args) {
//            userInfoPresenter.reqeust("Bearer " + token);
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }
}
